﻿<!doctype html>
<html>
<head>
<#include "/part_meta.ftl">
<title>购物车-迈小众</title>
<link href="${st.resDomain}/css/style.css" rel="stylesheet" type="text/css">
<link href="${st.resDomain}/css/store.css" rel="stylesheet" type="text/css">
<link href="${st.resDomain}/css/autostyle.css" rel="stylesheet" type="text/css">
<link href="${st.resDomain}/css/cart.css" rel="stylesheet" type="text/css">
<style type="text/css">

.stroll {
	text-align: center;
}

.stroll a {
	color: #FFF;
	border-radius: 3px;
	padding: 4px 12px;
	background: none repeat scroll 0% 0% #D11E1E;
	display: inline-block;
	line-height: 1.2em;
}
body {
	background-color: #EAF0F2;
}
</style>
</head>

<body>
<!--头部-->
<header>
  <div class="boxw store">
    <div class="header">
      <span class="f_left"><a href="javascript:history.back();" class="back"></a></span>
      <span class="title">购物车</span>
      <span class="f_right"><a class="more"></a></span>
    </div>
    <div class="more">
      <a href="${st.domain}/search" class="icon-search">搜索</a>
      <a href="${st.domain}/category" class="icon-fl">类目</a>
      <a href="${st.userDomain}/" class="icon-user">个人中心</a>
      <a href="${st.domain}/" class="icon-home">主页</a>
    </div>
  </div>
</header>
<!--头部 END-->

<#if shoppingCarts?size gt 0>
<div class="main boxw" style="padding-bottom:3.5rem;">
 <form action="${st.userDomain}/order/add" method="post" id="cartForm">
  <div class="shoppingcart">
   <#list shoppingCarts as sc>
    <#assign store = sc.store>
     <div class="shopname f_clear"><a name="checkWeek" class="checkbox" id="cartId_${sc.cartId}"></a><span>${store.name}</span></div>
     <div class="list">
      <#list sc.shoppings as s>
      <#assign g = s.storeGoods>
       <div class="good f_clear" id="listItem_${s.shoppingId}">
        <div class="left"><a name="checkWeek" class="checkbox" id="shoppingId_${sc.cartId}_${s.shoppingId}"></a></div>
        <div class="right">
          <div class="info f_clear"><img src="${st.resDomain}/images/loading.gif" data="${g.pic}"/><span><h2>${g.title}</h2><h3><strong>￥${g.price}</strong> <#if g.atDiscount><del>${g.originalPrice}</del></#if>  /${g.unit}</h3></span></div>
          <div id="data_goods_${s.shoppingId}_cart_${g.sellMin}_${g.sellExt}_${g.stock}_${g.unit}_${g.sellUnit}_${g.price}_${g.atDiscount}_${g.unitConvert}_${g.stockConvert}" style="display:none;">
	  	 	<span id="hbCount_${s.shoppingId}">0</span>
	  	  </div>
          <div class="cz f_clear"><a class="prv" id="bCountBt_m_${s.shoppingId}_cart"></a><input type="text" value="${s.buyCount}" id="bCount_${s.shoppingId}_cart"><span><#if g.sellUnit??>${g.sellUnit}<#else>${g.unit}</#if></span><a class="add" id="bCountBt_a_${s.shoppingId}_cart"></a><a class="del" id="delOneBt_${s.shoppingId}">删除</a></div>
        </div>
       </div>
      </#list>
    </div>
   </#list>
  </div>
 </form> 
</div>
<!--tj-->
<div class="tj">
  <div class="boxw">
    <div><a href="javascript:;" class="btn" id="addOrderBt"><span class="left">总计：<strong id="totalPrice">￥0</strong></span><span class="right">去结算</span></a></div>
  </div>
</div>
<!--tj end-->
<#else>
<div class="main boxw stroll">
 <p>购物车空空如也，快去选购吧</p>
 <a href="${st.domain}/">去逛逛</a>
</div>
</#if>
<script src="${st.resDomain}/scripts/jquery-1.9.1.min.js"></script>
<script src="${st.resDomain}/scripts/com.js"></script>
<script src="${st.resDomain}/scripts/shopping.js" type="text/javascript"></script>
<script type="text/javascript">
	
	// 删除购物车请求地址
	var deletePath = g_userDomain + "/shopping/delDo/json";
	
	//事件: 全选店铺物品
	$("[id^='cartId_']").click(function() {
		var $this = $(this);
		var cartId = ($this.attr("id").split("_"))[1];
		if ($this.hasClass("check")) { 
   	      $this.removeClass("check"); 
  	      $("[id^='shoppingId_" + cartId + "_']").removeClass("check");
 	    }else{ 
		  $this.addClass('check');
    	  $("[id^='shoppingId_" + cartId + "_']").addClass("check");
   	   } 
   	   stat();
	});

	// 事件: 勾选物品
	$("[id^='shoppingId_']").click(function() {
		var $this = $(this);
		var cartId = ($this.attr("id").split("_"))[1];
		if ($this.hasClass("check")) { 
            $this.removeClass("check");
            // 如果全选店铺商品状态下不勾选某种商品，则去掉店铺全选商品状态
            var $cartCB = $("#cartId_" + cartId);
            if ($cartCB.hasClass("check")) {
            	$cartCB.removeClass("check")
            }
        }else{ 
        	$this.addClass("check"); 
        } 
		stat();
	});
	
	function stat() {
		var totalPrice = 0;
		$("#totalPrice").html("￥" + 0);
		$("[id^='shoppingId_'].check").each(function() {
			var shoppingId = $(this).attr("id").split("_")[2];
			var $datas = $("[id^='data_goods_" + shoppingId + "']");
			var datas = $datas.attr("id").split("_");
			// 购买量
			var buyCount = $("#bCount_" + datas[2] + "_" + datas[3]).val();
			buyCount = parseFloat(buyCount);
				
			// 计费单位与参考单位不一样
			if (datas[8] != "" && datas[8] != datas[7]) {
				buyCount = buyCount * datas[11];
			}
				
			// 小计
			var subtotal = buyCount * datas[9];
			
			totalPrice += subtotal;
				
			$("#totalPrice").html("￥" + totalPrice.toFixed(1) + "0");
		});
	}
	
	// 事件：去结算
	$("#addOrderBt").click(function() {
		if ($("[id^='shoppingId_'].check").length == 0) {
			showMsg("请选择要结算的商品", 'error');
			return;
		}
		var $form = $("#cartForm");
		var cartIndex = -1;
		$("[id^='shoppingId_'].check").each(function(i, el) {
			var vars = $(this).attr("id").split("_");
			var cartId = vars[1];
			var shoppingId = vars[2];
			if ($("#shoppingCart_" + cartId).length == 0) {
				cartIndex++;
				$form.append("<input type='hidden' id='shoppingCart_" + cartId + "' name='shoppingCarts[" + cartIndex + "].cartId' value='" + cartId + "' />");
			}
			$form.append("<input type='hidden' name='shoppingCarts[" + cartIndex + "].shoppingIds' value='" + shoppingId + "' />");
		});
		$form.submit();
	});
	
	mxz.shoppingCart.buyCountChangeAfter(function() {
		stat();
	});
	
	// 事件：删除购物车商品
	$("[id^='delOneBt_']").click(function() {
		if (confirm("确定要删除这条商品吗？")) {
			var shoppingId = $(this).attr("id").split("_")[1];
			showMsg("正在删除...", "load");
			$.post(deletePath, {shoppingId: shoppingId}, function(data) {
				if (data.status === 0) {
					showMsg(data.message, 'pass');
					// 刷新购物车物品数量缓存值
					shoppingCount(true);
					$("#listItem_" + shoppingId).remove();
					// 重新统计勾选商品总价
					stat();
				} else {
					showMsg(data.message, 'error');
				}
			});
		}
	});
	
</script>	
</body>
</html>